﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using BLL.Common;
using EMPower.Utility;
using System.Web.UI.HtmlControls;

namespace App
{
    public partial class BBS_ThreadInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal commentuserinfo = (Literal)e.Item.FindControl("commentuserinfo");
                Literal labcommentbody = (Literal)e.Item.FindControl("labcommentbody");
                HtmlGenericControl lbtneditcomment = (HtmlGenericControl)e.Item.FindControl("pinlunClick1");
                UserPostInfo p = e.Item.DataItem as UserPostInfo;

                if (p != null && p.UserPostId > 0)
                {
                    if (commentuserinfo != null)
                    {
                        commentuserinfo.Text = "<a href='#'>" + (string.IsNullOrEmpty(p.NickName) ? p.UserName : p.NickName.ToString()) + "</a> 发表于" + p.DateCreated.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    if (labcommentbody != null)
                    {
                        labcommentbody.Text = p.Body;
                    }
                    if (lbtneditcomment != null && AppAuthHelper.SessionCheck()&& AppAuthHelper.CurrentUserPassportId > 0 && (AppAuthHelper.CurrentUserInfo.ProfileInfo.UserType == 3 || AppAuthHelper.CurrentUserPassportId == p.UserPassportId))
                    {
                        lbtneditcomment.Visible=true;
                        
                    }
                }


            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptCommentList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }

        protected void rptCommentList_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {

            if (e.CommandName == "lbEdit")//编辑评论
            {
                //LinkButton lbtn = (LinkButton)sender;
                //RepeaterItem ri = (RepeaterItem)lbtn.Parent;
                string userPostIdStr = e.CommandArgument as string;
                long userPostId = 0;
                if (long.TryParse(userPostIdStr, out userPostId))
                {
                    Response.Redirect("EditUserPost.aspx?"+Contexts.ThreadId+"="+Contexts.GetEncrypt(Contexts.GetAPPThreadId("/BBS/ThreadList.aspx").ToString())+"&"+Contexts.UserPostId+"="+Contexts.GetEncrypt(userPostIdStr));
                }

            }
            else if (e.CommandName == "lbDelete")//编辑评论
            {
                //LinkButton lbtn = (LinkButton)sender;
                //RepeaterItem ri = (RepeaterItem)lbtn.Parent;
                string userPostIdStr = e.CommandArgument as string;
                long userPostId = 0;
                if (long.TryParse(userPostIdStr, out userPostId))
                {
                    //Response.Redirect("EditUserPost.aspx?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(Contexts.GetAPPThreadId("/BBS/ThreadList.aspx").ToString()) + "&" + Contexts.UserPostId + "=" + Contexts.GetEncrypt(userPostIdStr));
                    
                    int result = BLL.BUserPost.getInstance().Delete(userPostId);
                    if (result > 0)
                    {
                        Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "删除成功", Contexts.ThisPageUrlWithParams);
                    }
                    else
                    {
                        Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "删除失败，请重试", Contexts.ThisPageUrlWithParams);
                    }
                }

            }
        }

        protected void btn_OnClick(object sender, EventArgs e)
        {
            if (AppAuthHelper.SessionCheck())
            {
                long paperId = 0;
                if (long.TryParse(this.hf_PaperId.Value, out paperId))
                {
                    UserPostInfo postInfo = new UserPostInfo();
                    postInfo.Body = this.content.Value;
                    postInfo.DateCreated = DateTime.Now;
                    postInfo.LastUpdate = postInfo.DateCreated;
                    postInfo.UserPaperId = paperId;
                    postInfo.UserPostId = IdHelper.GetLongId();
                    postInfo.Status = 1;
                    postInfo.ToPostId = 0;
                    postInfo.ToUserPassportId = 0;
                    postInfo.UserPassportId = AppAuthHelper.CurrentUserPassportId;

                    if (BLL.BUserPost.getInstance().Insert(postInfo))
                    {

                        Community.Utility.JavaScriptHelper.MessageBox(this, "发表评论成功");
                        this.content.Value = string.Empty;
                        bindPost();
                    }
                    else
                    {
                        Community.Utility.JavaScriptHelper.MessageBox(this, "发表评论失败,请重试");
                    }

                }
                else
                {
                    Community.Utility.JavaScriptHelper.MessageBox(this, "请刷新页面重试");
                }
            }
            else
            {
                Community.Utility.JavaScriptHelper.ConfirmBox(this, "请登录后发表评论，确定登录？", AppAuthHelper.GetLoginUrl(), Contexts.ThisPageUrlWithParams);
            }
        }
        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            bindPost();
        }

        protected void btn_EditPaper(object sender, EventArgs e)
        {
            Response.Redirect("AddThread.aspx?" + Contexts.ThreadId + "=" + Contexts.GetEncrypt(Contexts.GetAPPThreadId("/BBS/ThreadList.aspx").ToString()));

        }

        protected void btn_DeletePaper(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hf_PaperId.Value.Trim()))
            {
                long paperId = 0;

                if (long.TryParse(this.hf_PaperId.Value.Trim(), out paperId))
                {
                    UserPaperInfo paper = BLL.BUserPaper.getInstance().GetPaperInfo(paperId);
                    paper.LastUpdate = DateTime.Now;
                    paper.Status = -1;
                    bool result = BLL.BUserPaper.getInstance().Update(paper);
                    if (result)
                    {
                        Community.Utility.JavaScriptHelper.LocationPageAfterMessageBox(this, "删除成功，即将返回列表页", Contexts.AppPath + "/BBS/ThreadList.aspx");
                    }
                }

            }
            Community.Utility.JavaScriptHelper.MessageBox(this, "请刷新页面重试");
        }
        private void pageInit()
        {
            long paperId = Contexts.GetAPPThreadId("/BBS/ThreadList.aspx");

            UserPaperInfo paper = BLL.BUserPaper.getInstance().GetPaperInfo(paperId);

            if (paper != null && paper.UserPaperId > 0 && paper.Status == 1)
            {
                lbl_Subject.Text = paper.Subject;
                lbl_DateTime.Text = paper.DateCreated.ToShortDateString();
                lbl_Body.Text = paper.Body;
                hf_PaperId.Value = paper.UserPaperId.ToString();
                if (AppAuthHelper.SessionCheck() && AppAuthHelper.CurrentUserPassportId > 0 && (AppAuthHelper.CurrentUserInfo.ProfileInfo.UserType == 3 || AppAuthHelper.CurrentUserPassportId == paper.UserPassportId))
                {
                    this.pinlunClick.Visible = true;
                }
                if (paper.IsAllowComment == 0)
                {
                    divComment.Visible = false;
                    divClientNoSee.Visible = false;

                }
                else
                {
                    divComment.Visible = true;
                    divClientNoSee.Visible = true;

                    //
                    bindPost();
                }
                if (this.lbtneditPaper != null && AppAuthHelper.SessionCheck() && AppAuthHelper.CurrentUserPassportId > 0 && (AppAuthHelper.CurrentUserInfo.ProfileInfo.UserType == 3 || AppAuthHelper.CurrentUserPassportId == paper.UserPassportId))
                {
                    lbtneditPaper.Visible = true;
                }

            }
            else
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "文章失效，即将回退到上级页面");
                Page.RegisterClientScriptBlock("", "<script>widow.location='" + Contexts.AppPath + "/PolicyPager/PolicyPagerList.aspx'</script>");
            }
        }


        private List<UserPostInfo> getPages(long paperId)
        {
            List<UserPostInfo> posts = BLL.BUserPost.getInstance().GetAllPostByUserPaperId(paperId); ;//BLL.BPost.getInstance().GetAllPostANDUserNameByPaperId(paperId);

            if (posts != null && posts.Count > 0)
            {
                this.anpOrders.RecordCount = posts.Count;
                List<UserPostInfo> tempPosts = new List<UserPostInfo>();
                tempPosts.AddRange(posts);
                int currentNum = 0;

                //分页数据处理
                foreach (UserPostInfo info in tempPosts)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        posts.Remove(info);
                    }
                }
            }

            return posts;
        }

        private void bindPost()
        {
            long paperId = 0;
            if (long.TryParse(this.hf_PaperId.Value, out paperId))
            {
                List<UserPostInfo> posts = getPages(paperId);
                if (posts != null)
                {
                    this.rptCommentList.DataSource = posts;
                    this.rptCommentList.DataBind();
                }
            }
            else
            {
                Community.Utility.JavaScriptHelper.MessageBox(this, "请刷新页面重试");
            }
        }
    }
}